package com.yl.immodule.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yl.immodule.R;
import com.yl.immodule.databinding.PopupContactActionsBinding;

public class ContactActionsPopup extends PopupWindow {
    
    private final PopupContactActionsBinding binding;
    private final ActionListener listener;
    private PopupStateListener stateListener;
    
    public ContactActionsPopup(Context context, ActionListener listener) {
        super(context);
        this.listener = listener;
        
        binding = PopupContactActionsBinding.inflate(LayoutInflater.from(context));
        setContentView(binding.getRoot());

        // 设置popup的属性
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        // 设置背景为完全透明
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        
        // 禁止点击外部自动关闭
        setOutsideTouchable(false);
        // 设置为不可获取焦点，这样背景界面控件可以继续接收点击事件
        setFocusable(false);
        
        // 设置动画，从底部弹出
        setAnimationStyle(R.style.BottomPopupAnimation);

        // 设置点击事件
        binding.btnAudioCall.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAudioCallClicked();
            }
            // 不再自动关闭，由外部控制关闭时机
        });
        
        binding.btnVideoCall.setOnClickListener(v -> {
            if (listener != null) {
                listener.onVideoCallClicked();
            }
            // 不再自动关闭，由外部控制关闭时机
        });
        
        binding.btnSendMessage.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSendMessageClicked();
            }
            // 不再自动关闭，由外部控制关闭时机
        });
        
        // 添加弹窗显示和消失的监听
        setOnDismissListener(() -> {
            if (stateListener != null) {
                stateListener.onPopupDismissed();
            }
        });
    }
    
    public void setPopupStateListener(PopupStateListener listener) {
        this.stateListener = listener;
    }
    
    public int getPopupHeight() {
        if (binding.getRoot().getHeight() > 0) {
            return binding.getRoot().getHeight();
        }
        // 如果还没有测量，手动测量一下
        binding.getRoot().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        return binding.getRoot().getMeasuredHeight();
    }
    
    public void updateSelectedCount(int count) {
        binding.tvSelectedCount.setText("已选择 " + count + " 位联系人");
        
        // 语音通话和视频通话均仅支持单人
        View videoCallView = binding.btnVideoCall;
        View audioCallView = binding.btnAudioCall;
        
        // 设置按钮启用状态
        videoCallView.setEnabled(count == 1);
        audioCallView.setEnabled(count == 1);
        
        // 获取视频图标和文字
        TextView videoText = (TextView) ((android.widget.LinearLayout)videoCallView).getChildAt(1);
        
        // 获取语音图标和文字
        TextView audioText = (TextView) ((android.widget.LinearLayout)audioCallView).getChildAt(1);
        
        if (count == 1) {
            // 启用状态 - 视频通话
            videoCallView.setAlpha(1.0f);
            if (videoText != null) {
                videoText.setTextColor(videoText.getContext().getResources().getColor(android.R.color.black));
            }
            
            // 启用状态 - 语音通话
            audioCallView.setAlpha(1.0f);
            if (audioText != null) {
                audioText.setTextColor(audioText.getContext().getResources().getColor(android.R.color.black));
            }
        } else {
            // 禁用状态 - 视频通话
            videoCallView.setAlpha(0.5f);
            if (videoText != null) {
                videoText.setTextColor(videoText.getContext().getResources().getColor(android.R.color.darker_gray));
            }
            
            // 禁用状态 - 语音通话
            audioCallView.setAlpha(0.5f);
            if (audioText != null) {
                audioText.setTextColor(audioText.getContext().getResources().getColor(android.R.color.darker_gray));
            }
        }
    }
    
    public void show(View anchor) {
        // 从底部弹出，使用Gravity.BOTTOM确保贴合底部
        showAtLocation(anchor, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        
        // 通知状态监听器弹窗已显示
        if (stateListener != null) {
            stateListener.onPopupShown(getPopupHeight());
        }
    }
    
    public interface ActionListener {
        void onAudioCallClicked();
        void onVideoCallClicked();
        void onSendMessageClicked();
    }
    
    public interface PopupStateListener {
        void onPopupShown(int height);
        void onPopupDismissed();
    }
} 